package com.qfedu.yanglao20admin.handicapped.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.yanglao20.common.CommonResult;
import com.qfedu.yanglao20.common.annotation.LogInfo;
import com.qfedu.yanglao20.common.utils.OssUtils;
import com.qfedu.yanglao20.entity.handicapped.Handicapped;
import com.qfedu.yanglao20admin.handicapped.param.AddHandicappedParam;
import com.qfedu.yanglao20admin.handicapped.param.ContractParam;
import com.qfedu.yanglao20admin.handicapped.param.HandicappedParam;
import com.qfedu.yanglao20admin.handicapped.service.HandicappedService;
import com.qfedu.yanglao20admin.handicapped.vo.HandicappedVO;
import com.qfedu.yanglao20admin.handicapped.vo.SelectHandicappedVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

/**
 * @author yujie
 * @data 2023/4/27 14:57
 */
@Api(tags = "残疾人管理")
@RestController
@RequestMapping("/disability")
public class HandicappedController {

    @Resource
    private HandicappedService handicappedService;

    @LogInfo
    @ApiOperation(value = "分页展示残疾人管理信息", notes = "分页展示残疾人管理信息")
    @GetMapping("/page")
    public CommonResult<HandicappedVO> page(HandicappedParam param) {
        Page<HandicappedVO> handicappedVOPage = handicappedService.pageHandicapped(param);
        return CommonResult.success(handicappedVOPage);
    }

    @LogInfo
    @ApiOperation(value = "添加残疾人以及亲属信息", notes = "添加残疾人以及亲属信息")
    @PostMapping("/add")
    public CommonResult<AddHandicappedParam> addHandicapped(@RequestBody AddHandicappedParam addHandicappedParam) {
        handicappedService.addHandicapped(addHandicappedParam);
        return CommonResult.success();
    }

    @LogInfo
    @ApiOperation(value = "根据id查询残疾人以及亲属信息", notes = "根据id查询残疾人以及亲属信息")
    @GetMapping("/query")
    public CommonResult<SelectHandicappedVO> queryHandicapped(@RequestParam @ApiParam("残疾人id") Integer id) {
        SelectHandicappedVO selectHandicappedVO = handicappedService.selectAllById(id);
        return CommonResult.success(selectHandicappedVO);
    }

    @LogInfo
    @ApiOperation(value = "根据id删除残疾人信息", notes = "根据id删除残疾人信息信息")
    @GetMapping("/delete")
    public CommonResult<Handicapped> deleteHandicapped(@RequestParam @ApiParam("残疾人id") Integer id) {
        handicappedService.deleteHandicapped(id);
        return CommonResult.success();
    }

    @LogInfo
    @ApiOperation(value = "修改该残疾人和亲属信息", notes = "修改该残疾人和亲属信息")
    @PostMapping("/update")
    public CommonResult<AddHandicappedParam> updateHandicapped(@RequestBody AddHandicappedParam param) {
        handicappedService.updateHandicapped(param);
        return CommonResult.success();
    }

    @LogInfo
    @ApiOperation(value = "修改服务状态", notes = "修改服务状态")
    @PostMapping("/ups")
    public CommonResult<Handicapped> updateStateFlag(@RequestParam @ApiParam("服务状态") Integer stateFlag,
                                                     @RequestParam @ApiParam("残疾人id") Integer id) {
        handicappedService.updateStateFlag(stateFlag, id);
        return CommonResult.success();
    }

    @LogInfo
    @ApiOperation(value = "修改合同时间", notes = "修改合同时间")
    @PostMapping("/contract")
    public CommonResult<ContractParam> updateCaseAffiliation(ContractParam param) {
        handicappedService.updateContract(param);
        return CommonResult.success();
    }

    @LogInfo
    @ApiOperation(value = "修改近照", notes = "修改近照")
    @PostMapping("/photos")
    public CommonResult<Handicapped> updatePhotosUrl(@RequestParam @ApiParam("近照路径") String photosUrl,
                                                     @RequestParam @ApiParam("残疾人id") Integer id) {
        handicappedService.updatePhotosUrl(photosUrl, id);
        return CommonResult.success();
    }

    @LogInfo
    @ApiOperation(value = "上传合同或近照", notes = "上传合同或近照")
    @PostMapping("/upload")
    public CommonResult<Handicapped> upload(MultipartFile file) {

        String path = null;

        String name = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replace("-", "");
        name = uuid + name.substring(name.lastIndexOf("."));

        try {
            path = OssUtils.uploadFile2OSS(file.getInputStream(), name);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return CommonResult.success(path);
    }
}
